<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <template #title>
        <van-button
          type="info"
          size="small"
          round
          icon="search"
          class="search"
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 文章列表子组件 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 汉堡按钮 -->
      <template #nav-right>
        <!-- 占位盒子 -->
        <div class="placeholder"></div>
        <div class="hamburger-btn" @click="isEditPopup = true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <!-- 编辑频道弹出层 -->
    <van-popup
      v-model="isEditPopup"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
      @close="closeFn"
    >
      <!-- 子组件 -->
      <channel-edit
        ref="channelEdit"
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  data () {
    return {
      // 激活的频道索引
      active: 0,
      // 用户的频道列表
      channels: [],
      // 编辑频道弹出层显隐
      isEditPopup: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  methods: {
    // 获取用户频道
    async loadChannels () {
      try {
        let channels = []
        // 用户登录
        if (this.user) {
          // 已登录，请求获取用户频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS')
          //    有，拿来使用
          if (localChannels) {
            channels = localChannels
          } else {
            //    没有，请求获取默认频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        // 用户频道数据保存到data里
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道数据失败')
      }
    },
    // 弹出层频道切换
    onUpdateActive (e) {
      // 更新激活的频道项
      this.active = e.index
      // 关闭编辑频道弹层
      this.isEditPopup = e.close
    },
    // 关闭弹出层
    closeFn () {
      this.$refs.channelEdit.isEdit = false
    }
  }
}
</script>

<style scoped lang='less'>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab {
      min-width: 200px;
      font-size: 28px;
      color: #777;
      border-right: 1px solid #edeff3;
      &:nth-last-child(4) {
        border-right: none !important;
      }
    }

    .van-tab--active {
      font-size: 30px;
      color: #333;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &::before {
        content: '';
        width: 1px;
        height: 60%;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 0;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
  }
}
</style>
